package Greedy;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PartitionLabels
 * @Description TODO
 * @Author lenovo
 * @Date 2023-06-26 10:08
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class PartitionLabels {

    /**
     * 示例 1：
     * 输入：s = "ababcbacadefegdehijhklij"
     * 输出：[9,7,8]
     * 解释：
     * 划分结果为 "ababcbaca"、"defegde"、"hijhklij" 。
     * 每个字母最多出现在一个片段中。
     * 像 "ababcbacadefegde", "hijhklij" 这样的划分是错误的，因为划分的片段数较少。
     * 示例 2：
     * <p>
     * 输入：s = "eccbbbbdec"
     * 输出：[10]
     *
     * @param s
     * @return
     */
    public List<Integer> partitionLabels(String s) {
        int[] index = new int[26];
        char[] charArray = s.toCharArray();
        for (int i = 0; i < charArray.length; i++) {
            index[charArray[i] - 'a'] = i;
        }
        int idx = 0;
        int last = -1;
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < charArray.length; i++) {
            idx = Math.max(idx, index[charArray[i] - 'a']);
            if (idx == i) {
                result.add(idx - last);
                last = i;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        PartitionLabels partitionLabels = new PartitionLabels();
        List<Integer> list = partitionLabels.partitionLabels("ababcbacadefegdehijhklij");
        for (Integer integer : list) {
            System.out.println(integer);
        }
    }
}